{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9b9978ef",
   "metadata": {},
   "source": [
    "---\n",
    "* 2023-11-8\n",
    "* 课程：Python语言\n",
    "* week08\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98431771",
   "metadata": {},
   "source": [
    "# 1.本周内容\n",
    "## 1.列表的数据处理（列表的方法）\n",
    "\n",
    "> 1. 查询数据\n",
    ">> 1. index() 按照索引值查询数据，并取出值\n",
    ">> 2. slice 切片方法取值\n",
    ">> 3. in 方法查询元素是否存在\n",
    "> 2. 增加数据\n",
    ">> 1. append() 增加在末尾\n",
    ">> 2. insert() 指定位置增加\n",
    "> 3. 删除数据\n",
    ">> 1. pop()指定位置删除\n",
    ">> 2. remove()指定结果(值)删除\n",
    ">> 3. clear()清空列表\n",
    "> 4. 修改数据\n",
    ">> 1. 找到index进行修改\n",
    "> 5. 其他方法\n",
    ">> 1. 排序：sort()reverse()\n",
    ">> 2. 复制copy()\n",
    "\n",
    "\n",
    "## 2.列表实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "564d87d5",
   "metadata": {},
   "source": [
    "## 1.1 列表的删除\n",
    "\n",
    "> 1. pop()\n",
    ">> 1. 默认删除列表中最后一位数据\n",
    ">> 2. 指定位置进行删除\n",
    ">> 3. 注意事项：避免index\n",
    "> 2. remove\n",
    ">> 1. 指定结果（值）删除\n",
    ">> 2. 注意事项：避免valueerror的出现，先检查元素是否存在 in\n",
    "> 3. clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "373a3e11",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1d2412e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e2994115",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李晓明'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d7d3b0a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李晓明'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "10365f32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "309b9668",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "82b982fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '小红']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b6a929b",
   "metadata": {},
   "source": [
    "### 2.remove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c7b86590",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cc4dcf0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a733cda2",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.remove(\"小红\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "90db8fc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '李晓明']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "16e36dcf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先查询是否存在该值\n",
    "\"小红\" in name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5d54da12",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m name_list\u001b[38;5;241m.\u001b[39mremove(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m小红\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "name_list.remove(\"小红\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07bee08c",
   "metadata": {},
   "source": [
    "## 3.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3069e829",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "14408703",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3812315d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1c8aa22",
   "metadata": {},
   "source": [
    "# 2.排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e1a61a62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 17, 19, 20, 21]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list = [18,19,18,17,19,20,21]\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4641dc7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "908d829d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[17, 18, 18, 19, 19, 20, 21]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5f24124b",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "027ad60b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[21, 20, 19, 19, 18, 18, 17]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7aac8bcc",
   "metadata": {},
   "source": [
    "# 3.复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "53c54de6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "eda2c874",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "存档_name_list = name_list\n",
    "存档_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f350a1e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"Jelex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "43405648",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明', 'Jelex']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8ca46d49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明', 'Jelex']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "存档_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "18a6005f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d239001c",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"Jelex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "8bc5ba8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70cf47e2",
   "metadata": {},
   "source": [
    "# 2.元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "90b4cbdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = 12345,54321,'hello!'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "918a75e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12345, 54321, 'hello!')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(t)\n",
    "print(type(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "98f1317f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12345, 54321, 'hello!')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (12345,54321,'hello!')\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "ad644e88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "316f1f71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12345, 54321)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c2a5f283",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.count(12345)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "b6ebb9a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2023, tm_mon=11, tm_mday=8, tm_hour=11, tm_min=35, tm_sec=50, tm_wday=2, tm_yday=312, tm_isdst=0)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 案例 \n",
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "21038861",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1699414558.8930748"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.time()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef85b549",
   "metadata": {},
   "source": [
    "# 实践："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e64260df",
   "metadata": {},
   "source": [
    "   购物车程序项目:\n",
    "```\n",
    "   要求: 1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "         2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "         3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "        \n",
    "         4、用户可以随时退出购买，退出时打印用户已购买的商品和支付宝余额。\n",
    "\n",
    "1. alipay = input ()\n",
    "\n",
    "商品列表= [\n",
    "\n",
    "]\n",
    "购物车 = []\n",
    "\n",
    "print (商品列表)\n",
    "\n",
    "2. user_ choose = input()\n",
    "\n",
    "if 商品价格 > alipay\n",
    "    1.\n",
    "else:\n",
    "    2.\n",
    "    \n",
    "3.购物车.append(购买到的商品)\n",
    "\n",
    "4."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7f03777",
   "metadata": {},
   "outputs": [],
   "source": [
    "alipay = input(\"请输入你的余额:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23e4a55f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3acbf51c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
